package com.music.mapper;

import com.music.pojo.Comment;
import com.music.pojo.CommentExample;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface CommentMapper {
    @SelectProvider(type=CommentSqlProvider.class, method="countByExample")
    long countByExample(CommentExample example);

    @DeleteProvider(type=CommentSqlProvider.class, method="deleteByExample")
    int deleteByExample(CommentExample example);

    @Delete({
        "delete from comment",
        "where cid = #{cid,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer cid);

    @Insert({
        "insert into comment (cid, text, ",
        "uid, create_time, mid, ",
        "parent_cid)",
        "values (#{cid,jdbcType=INTEGER}, #{text,jdbcType=VARCHAR}, ",
        "#{uid,jdbcType=INTEGER}, #{createTime,jdbcType=DATE}, #{mid,jdbcType=INTEGER}, ",
        "#{parentCid,jdbcType=INTEGER})"
    })
    int insert(Comment row);

    @InsertProvider(type=CommentSqlProvider.class, method="insertSelective")
    int insertSelective(Comment row);

    @SelectProvider(type=CommentSqlProvider.class, method="selectByExample")
    @Results({
        @Result(column="cid", property="cid", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="text", property="text", jdbcType=JdbcType.VARCHAR),
        @Result(column="uid", property="uid", jdbcType=JdbcType.INTEGER),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.DATE),
        @Result(column="mid", property="mid", jdbcType=JdbcType.INTEGER),
        @Result(column="parent_cid", property="parentCid", jdbcType=JdbcType.INTEGER)
    })
    List<Comment> selectByExample(CommentExample example);

    @Select({
        "select",
        "cid, text, uid, create_time, mid, parent_cid",
        "from comment",
        "where cid = #{cid,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="cid", property="cid", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="text", property="text", jdbcType=JdbcType.VARCHAR),
        @Result(column="uid", property="uid", jdbcType=JdbcType.INTEGER),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.DATE),
        @Result(column="mid", property="mid", jdbcType=JdbcType.INTEGER),
        @Result(column="parent_cid", property="parentCid", jdbcType=JdbcType.INTEGER)
    })
    Comment selectByPrimaryKey(Integer cid);

    @UpdateProvider(type=CommentSqlProvider.class, method="updateByExampleSelective")
    int updateByExampleSelective(@Param("row") Comment row, @Param("example") CommentExample example);

    @UpdateProvider(type=CommentSqlProvider.class, method="updateByExample")
    int updateByExample(@Param("row") Comment row, @Param("example") CommentExample example);

    @UpdateProvider(type=CommentSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Comment row);

    @Update({
        "update comment",
        "set text = #{text,jdbcType=VARCHAR},",
          "uid = #{uid,jdbcType=INTEGER},",
          "create_time = #{createTime,jdbcType=DATE},",
          "mid = #{mid,jdbcType=INTEGER},",
          "parent_cid = #{parentCid,jdbcType=INTEGER}",
        "where cid = #{cid,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Comment row);

    @SelectProvider(type=CommentSqlProvider.class, method="selectByExample")
    @Results({
            @Result(column="cid", property="cid", id=true),
            @Result(column="text", property="text"),
            @Result(column="create_time", property="createTime"),
            @Result(column="parent_cid", property="parentCid"),
            @Result(column = "cid",property = "comments",many = @Many(select = "com.music.mapper.CommentMapper.findChildrenComments")),
            @Result(column="uid", property="userInfo",one = @One(select = "com.music.mapper.UserInfoMapper.findByUid")),
            @Result(column="mid", property="song",one = @One(select = "com.music.mapper.SingleSongMapper.findByMid")),
    })
    List<Comment> findWithUserAndSingleSong(CommentExample example);


    @Select("select * from comment where parent_cid = #{cid} order by create_time desc")
    @Results({
            @Result(column="cid", property="cid", id=true),
            @Result(column="text", property="text"),
            @Result(column="create_time", property="createTime"),
            @Result(column="parent_cid", property="parentCid"),
            @Result(column="uid", property="userInfo",one = @One(select = "com.music.mapper.UserInfoMapper.findByUid")),
            @Result(column = "cid",property = "comments",many = @Many(select = "com.music.mapper.CommentMapper.findChildrenComments")),
    })
    List<Comment> findChildrenComments(int cid);


    @Select("select * from comment where mid = #{mid} and parent_cid is null order by create_time desc")
    @Results({
            @Result(column="cid", property="cid", id=true),
            @Result(column="text", property="text"),
            @Result(column="create_time", property="createTime"),
            @Result(column="parent_cid", property="parentCid"),
            @Result(column = "cid",property = "comments",many = @Many(select = "com.music.mapper.CommentMapper.findChildrenComments")),
            @Result(column="uid", property="userInfo",one = @One(select = "com.music.mapper.UserInfoMapper.findByUid")),
    })
    List<Comment> findWithUser(String mid);

    @Insert("insert into comment(mid,uid,create_time,text) values (#{mid},#{uid},now(),#{text})")
    void addCommentToSong(@Param("mid") Integer mid,@Param("uid") Integer uid,@Param("text") String commentText);

    @Insert("insert into comment(mid,uid,create_time,text,parent_cid) values (#{mid},#{uid},now(),#{text},#{cid})")
    void addCommentToUser(@Param("mid") Integer mid,@Param("cid") Integer cid,@Param("uid") Integer uid,@Param("text") String commentText);
}